package us.team.openAnAppAtRandom;

import android.app.Activity;
import android.content.Intent;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

public class ScreenRecordActivity extends Activity {
    private static final int SCREEN_RECORD_REQUEST_CODE = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 直接请求录屏权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            MediaProjectionManager mediaProjectionManager = 
                (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
            Intent screenCaptureIntent = mediaProjectionManager.createScreenCaptureIntent();
            startActivityForResult(screenCaptureIntent, SCREEN_RECORD_REQUEST_CODE);
        } else {
            Toast.makeText(this, "您的设备不支持录屏功能", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        
        if (requestCode == SCREEN_RECORD_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                Toast.makeText(this, "录屏已开始", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "录屏权限被拒绝", Toast.LENGTH_SHORT).show();
            }
        }
        
        // 无论成功还是失败都关闭Activity
        finish();
    }
}